package com.xinfu.newproject.model.meter;

import com.xinfu.newproject.apiservice.JsNetworkService;
import com.xinfu.newproject.bean.meter.bean.AddUserMeterBean;
import com.xinfu.newproject.bean.meter.bean.CheckMeterCodeBean;
import com.xinfu.newproject.bean.meter.bean.MeterListBean;
import com.xinfu.newproject.bean.meter.body.AddUserMeterBody;
import com.xinfu.newproject.bean.meter.body.CheckMeterCodeBody;
import com.xinfu.newproject.bean.meter.body.MeterListBody;
import com.xinfu.newproject.callback.MyCallBack;
import com.xinfu.newproject.model.IModel;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class MeterModel implements IModel.IMeterModel {

    /**
     * 手动添加水电表
     *
     * @param addUserMeterBody
     * @param callBack
     */
    @Override
    public void addUserMeter(AddUserMeterBody addUserMeterBody, MyCallBack<AddUserMeterBean> callBack) {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", addUserMeterBody.getUserId());
        map.put("projectId", addUserMeterBody.getProjectId());
        map.put("meterId", addUserMeterBody.getMeterId());

        JsNetworkService.getInstance2().getIJsNetworkService()
                .addUserMeter(addUserMeterBody.getHeader(), map)
                .throttleFirst(1, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<AddUserMeterBean>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        Disposable mDisposable; //用于Activity销毁时停止执行
                        mDisposable = d;
                        callBack.onDispose(mDisposable);
                    }

                    @Override
                    public void onNext(@NonNull AddUserMeterBean addUserMeterBean) {

                        callBack.onSuccess(addUserMeterBean);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        callBack.onError("Server Error", "网络异常，请稍后再试");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 校验meterCode
     *
     * @param checkMeterCodeBody
     * @param callBack
     */
    @Override
    public void checkMeterCode(CheckMeterCodeBody checkMeterCodeBody, MyCallBack<CheckMeterCodeBean> callBack) {
        Map<String, Object> map = new HashMap<>();
        map.put("meterCode", checkMeterCodeBody.getMeterCode());
        map.put("projectId", checkMeterCodeBody.getProjectId());
        map.put("meterType", checkMeterCodeBody.getMeterType());

        JsNetworkService.getInstance2().getIJsNetworkService()
                .checkMeterCode(checkMeterCodeBody.getHeader(), map)
                .throttleFirst(1, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<CheckMeterCodeBean>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        Disposable mDisposable; //用于Activity销毁时停止执行
                        mDisposable = d;
                        callBack.onDispose(mDisposable);
                    }

                    @Override
                    public void onNext(@NonNull CheckMeterCodeBean checkMeterCodeBean) {

                        callBack.onSuccess(checkMeterCodeBean);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        callBack.onError("Server Error", "网络异常，请稍后再试");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 查询所有水电表
     *
     * @param meterListBody
     * @param callBack
     */
    @Override
    public void meterList(MeterListBody meterListBody, MyCallBack<MeterListBean> callBack) {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", meterListBody.getUserId());
        map.put("meterType", meterListBody.getMeterType());

        JsNetworkService.getInstance2().getIJsNetworkService()
                .meterList(map)
                .throttleFirst(1, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<MeterListBean>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        Disposable mDisposable; //用于Activity销毁时停止执行
                        mDisposable = d;
                        callBack.onDispose(mDisposable);
                    }

                    @Override
                    public void onNext(@NonNull MeterListBean meterListBean) {

                        callBack.onSuccess(meterListBean);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        callBack.onError("Server Error", "网络异常，请稍后再试");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 水电表支付
     *
     * @param meterListBody
     * @param callBack
     */
    @Override
    public void meterPay(MeterListBody meterListBody, MyCallBack<MeterListBean> callBack) {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", meterListBody.getUserId());
        map.put("meterType", meterListBody.getMeterType());

        JsNetworkService.getInstance().getIJsNetworkService()
                .meterPay(meterListBody.getHeader(), map)
                .throttleFirst(1, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<MeterListBean>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        Disposable mDisposable; //用于Activity销毁时停止执行
                        mDisposable = d;
                        callBack.onDispose(mDisposable);
                    }

                    @Override
                    public void onNext(@NonNull MeterListBean meterListBean) {
                        callBack.onSuccess(meterListBean);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        callBack.onError("Server Error", "网络异常，请稍后再试");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

}
